package cn.mengfly.testnrz.crazyIm.webserver.controller;

import cn.mengfly.testnrz.crazyIm.nettyserver.ImNode;
import cn.mengfly.testnrz.crazyIm.webserver.ImLoadBalance;
import cn.mengfly.testnrz.im.client.bean.User;
import cn.mengfly.testnrz.service.UserService;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@EnableAutoConfiguration
@RestController
@RequestMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping(value = "/login/{username}/{password}")
    public LoginBack loginAction(@PathVariable("username") String username, @PathVariable("password") String password) {
        User user = new User();
        user.setUid(username);
        user.setToken(password);
        User loginUser = userService.login(user);
        LoginBack loginBack = new LoginBack();
        ImNode bestWorker = ImLoadBalance.INSTANCE.getBestWorker();
        loginBack.setImNode(bestWorker);
        loginBack.setUser(loginUser);
        loginBack.setToken(loginUser.getUserId().toString());
        return loginBack;
    }


}
